/**
 *
 * @param speedInBytesPerSecond 上传速度（字节/秒）
 * @returns 上传速度
 */
const formatSpeed = (speedInBytesPerSecond) => {
    const units = ['B/s', 'Kb/s', 'Mb/s', 'Gb/s', 'Tb/s'];
    let index = 0;
    let speed = speedInBytesPerSecond;

    while (speed >= 1024 && index < units.length - 1) {
        speed /= 1024;
        index++;
    }

    return `${speed.toFixed(2)} ${units[index]}`;
}

function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0 B';

    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    const i = Math.floor(Math.log(bytes) / Math.log(k));

    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

export {
    formatSpeed,
    formatBytes
}